<template>
  <div class="dashboard-container">
    <el-form ref="form" :model="form" label-width="120px">

      <el-form-item label="LOGO">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="form.icon" :src="form.icon" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>

      <el-form-item label="物业名称">
        <el-input v-model="form.commName" />
      </el-form-item>

      <el-form-item label="物业简介">
        <el-input v-model="form.introduce" type="textarea" resize="none" :autosize="{ minRows: 2, maxRows: 6 }" />
      </el-form-item>

      <el-form-item label="服务项目">
        <el-input v-model="form.service" type="textarea" resize="none" :autosize="{ minRows: 2, maxRows: 6 }" />
      </el-form-item>

      <el-form-item label="招聘信息">
        <tableJobs />
      </el-form-item>

      <el-form-item label="联系方式">
        <tableContact />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { getWuyeInfo, setWuyeInfo } from '@/api/wuye'
import tableJobs from './TableJobs'
import tableContact from './TableContact'
export default {
  name: 'Dashboard',
  components: {
    tableJobs,
    tableContact
  },
  data() {
    return {
      form: {
        id: 0,
        commName: '',
        introduce: '',
        service: '',
        icon: ''
      },
      jobs: [],
      contact: []
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  created() {
    getWuyeInfo().then(res => {
      console.log(res)
      this.form = Object.assign(this.form, res.data)
    })
  },
  methods: {
    onSubmit() {
      console.log('submit!', this.form)
      setWuyeInfo('红星物业', this.form).then(res => {
        console.log(res)
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleEdit(index, row) {
      console.log(index, row)
    },
    handleDelete(index, row) {
      console.log(index, row)
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>

<style lang="scss">
.dashboard-container {
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
  }
  .avatar {
    width: 80px;
    height: 80px;
    display: block;
  }
}
</style>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
